package model

import (
	"server/share/config"
	"time"
)

const (
	AttachUnitLv       = "lv"
	AttachUnitExp      = "exp"
	AttachUnitSlv      = "slv"
	AttachUnitRlv      = "rlv"
	AttachUnitMood     = "mood"
	AttachUnitLove     = "love"
	AttachUnitKill     = "kill"
	AttachUnitDeath    = "death"
	AttachUnitPendent  = "pendent"
	AttachUnitChipList = "chipList"
	AttachUnitCostList = "costList"
	AttachUnitSkill    = "skill"
)

type UnitMood struct {
	Val  int32 `json:"val,omitempty" bson:"val"`   //心情
	Time int64 `json:"time,omitempty" bson:"time"` //Date.now()
}
type UnitDeath struct {
	Time   int64 `json:"t,omitempty" bson:"t"` //放逐时间  MS
	Value  int32 `json:"v,omitempty" bson:"v"` //放逐之地 累计时间，从放逐之地拿出来,还没复活时累计时间
	Status int8  `json:"s,omitempty" bson:"s"` //0 -正常，1-死亡，2-放逐之地
}

//
//type Unit struct {
//	Slv int32 `json:"slv,omitempty" bson:"slv"` //星级 包括初始星级
//	Rlv int32 `json:"rlv,omitempty" bson:"rlv"` //阶级
//	Exp int64 `json:"exp,omitempty" bson:"exp"` //经验
//	//DeadTime  int64             `json:"deadTime,omitempty" bson:"deadTime"`   //死亡时间
//	Mood    *UnitMood       `json:"mood,omitempty" bson:"mood"`       ////心情值
//	Love    int8            `json:"love,omitempty" bson:"love"`       //0表示默认值，1表示很爱这个单位
//	Kill    int32           `json:"kill,omitempty" bson:"kill"`       //击杀数量
//	Death   *UnitDeath      `json:"death,omitempty" bson:"death"`     //死亡信息 TODO新加
//	Pendant map[int32]int32 `json:"pendant,omitempty" bson:"pendant"` //挂件
//	//SkillList map[int32]int32   `json:"skillList,omitempty" bson:"skillList"` //技能列表，废弃
//	ChipList map[int32]string `json:"chipList,omitempty" bson:"chipList"` //芯片列表, pos->chip.OID
//	CostList map[int32]int64  `json:"costList,omitempty" bson:"costList"` //培养消耗
//	Skill    []int32          `json:"skill,omitempty" bson:"skill"`       //技能等级，新版，和配置表中的技能列表对应
//}

// 加心情值
func (this *UnitMood) AddVal(v int32) {
	this.Val += v
	moodMaxValue := config.Data.Base["moodMaxValue"]
	if this.Val > moodMaxValue.Val {
		this.Val = moodMaxValue.Val
	}
}

func (this *Items) GetUnitMood(now ...time.Time) (r *UnitMood) {
	r = &UnitMood{}
	if err := this.Attach.Unmarshal(AttachUnitMood, r); err != nil {
		return
	}

	var t time.Time
	if len(now) > 0 {
		t = now[0]
	} else {
		t = time.Now()
	}
	//结算一次
	moodMaxValue := config.Data.Base["moodMaxValue"]
	if r.Val >= moodMaxValue.Val {
		r.Time = t.UnixMilli()
		return r
	}
	moodAddSpeed := config.Data.Base["moodAddSpeed"]
	addSpeed := int64(moodAddSpeed.Val * 1000)
	subTime := t.UnixMilli() - r.Time
	addNum := subTime / addSpeed
	if addNum <= 0 {
		return r
	}
	r.Val += int32(addNum)
	if r.Val >= moodMaxValue.Val {
		r.Val = moodMaxValue.Val
		r.Time = t.UnixMilli()
	} else {
		r.Time += addNum * addSpeed
	}

	return r
}

func (this *Items) GetUnitDeath() *UnitDeath {
	r := &UnitDeath{}
	if err := this.Attach.Unmarshal(AttachUnitDeath, r); err != nil {
		return r
	}
	return r
}

func (this *Items) GetUnitCost() map[int32]int64 {
	r := make(map[int32]int64)
	_ = this.Attach.Unmarshal(AttachUnitCostList, r)
	return r
}
func (this *Items) GetUnitPendant() map[int32]int32 {
	r := make(map[int32]int32)
	_ = this.Attach.Unmarshal(AttachUnitPendent, r)
	return r
}

//func (this *Items) GetUnitSkill() map[int32]int32 {
//	var vs []int32
//	_ = this.Attach.Unmarshal(AttachUnitSkill, &vs)
//	r := make(map[int32]int32)
//	c := config.Data.SkillSets[this.IID]
//	l := len(vs)
//	for i, k := range c.Skills {
//		if k > 0 {
//			if i < l {
//				r[k] = vs[i]
//			} else {
//				r[k] = 1
//			}
//		}
//	}
//	return r
//}
//
//func (this *Items) SetUnitSkill(d map[int32]int32) []int32 {
//	c := config.Data.SkillSets[this.IID]
//	vs := make([]int32, len(c.Skills))
//	for i, k := range c.Skills {
//		vs[i] = d[k]
//	}
//	return vs
//}
